<template>
	<view id="page">
		<nav-bar :navH="navH" :isTabar="false" :isHome="false" :pageName="title"></nav-bar>
		<view class="banner">
			<image :src="banner" mode="widthFix" lazy-load="true" @error="imgError" v-if="banner"></image>
			<image src="../../static/default.jpg" mode="widthFix" lazy-load="true" v-else></image>
		</view>
		<!-- 图文列表 -->
	    <view class="classify clearfloat">
			<view :class="['class-i', isOn === 0 ? 'on' : '']" @click='switchMenu(0)'>全部</view>
			<view :class="['class-i', isOn === 1 ? 'on' : '']" @click='switchMenu(1)'>视频({{spnum}})</view>
			<view :class="['class-i', isOn === 2 ? 'on' : '']" @click='switchMenu(2)'>图文({{zxnum}})</view>
	    </view>
		<!-- 图文列表 -->
		<!-- <pic-txt :lists="lists" v-if="hasRes"></pic-txt> -->
		<!-- <view class="nocont" v-else>暂无内容</view> -->
		<view class="nomore" v-if="isLoad"><image src='../../static/nomore_img.png' mode="widthFix"></image><text>没有更多了~</text></view>
	</view>
</template>

<script>
	// import PicTxt from '../../components/pictxt/index.vue'
	import app from '../../common/index.js'
	export default {
		data() {
			return {
				title: '',
				navH: null,
				userInfo: {},
				isOn: 0,
				hasRes: true,
				isLoad: false,
				page: 1,
				size: 10,
				handedData: [[],[],[]],  // 分类过的数据
				lists: [],
				typeId: '',
				spnum: '',
				zxnum: '',
				banner: '',  // Banner
			}
		},
		onLoad(options) {
			this.title = options.n;
			this.navH = this.$store.state.navHeight;
			this.typeId = options.id;
			// 获取本地用户信息
			this.userInfo = uni.getStorageSync('uinfo');
			this.getBanner();
			this.getPicTxt();
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.page = 1;
			this.lists = [];
			this.handedData = [[],[],[]];
			// 获取本地用户信息
			this.userInfo = uni.getStorageSync('uinfo');
			this.getPicTxt();
			uni.stopPullDownRefresh();
		},
		// 上划加载
		onReachBottom() {
			if(!this.isLoad) {
				this.page = this.page+1;
				this.getPicTxt();
			}
		},
		// 分享转发
		onShareAppMessage() {
			return {
				title: '爱美指南针',
				path: 'pages/index/index',
			}
		},
		components: {
			// PicTxt,
		},
		methods: {
			/**
			 * @des 获取Banner
			 */
			getBanner() {
				app.request({
					url: 'entry/wxapp/Banner',
					data: {id: this.typeId}
				}, (ret) => {
					console.log(ret);
					let d = ret.data;
					this.banner = d.icon;
				})
			},
			/**
			 * @des 获取图文列表
			 */
			getPicTxt() {
				app.request({
					url: 'entry/wxapp/TypeList',
					data: {
						id: this.typeId,
						user_id: this.userInfo.id,
					}
				}, (ret) => {
					console.log(ret);
					let d = ret.data, totalArr = this.handedData, len = d.data.length;
					this.zxnum = d.zx_num; this.spnum = d.video_num;
					if (d.total > 0) {
						this.hasRes = true;
						this.isLoad = len < this.size ? true : false;
						d.data.forEach((item, index) => {  // 此处做数据分类处理
							totalArr[0].push(item);
							if(item.is_video === '1') {
								totalArr[1].push(item);
							}else if(item.is_video === '0') {
								totalArr[2].push(item);
							}
						})
						this.handedData = totalArr;
						this.switchMenu(this.isOn);
					} else {
						this.hasRes = false;
					}
				})
			},
			/**
			 * @des 切换菜单
			 */
			switchMenu(i) {
				this.isOn = i;
				this.lists = this.handedData[i];
			},
			/**
			 * @des 处理错误图片
			 */
			imgError() {
				this.banner = '../../static/default.jpg';
			}
		}
	}
</script>

<style>
	.banner {max-height: 415upx; overflow: hidden;}
	/** 图文分类菜单 */
	.classify {width: 100%; padding: 0 20upx; background-color: #fff;}
	.classify .class-i {float: left; margin: 0 20upx; padding: 20upx 10upx; line-height: 1; font-size: 30rpx; color: #888;}
	.classify .class-i.on {color: #82d7d0; border-bottom: 2px solid #82d7d0;}
</style>
